package thread.method;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-03-06
 * Time:16:14
 */
public class Method2 {
    /*
    static void yield()  线程礼让----让出CPU,让其他线程先执行 (是否礼让由CPU决定,根据CPU是否忙碌----忙碌时礼让可能更高)
    void join() throws InterruptedException  线程插队----插队的线程一旦插队成功,则肯定先执行完插入的线程的所有任务
     */
    public static void main(String[] args) throws InterruptedException {
        T2 t2 = new T2();
        t2.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("main执行第" + (i + 1) + "次");
            Thread.sleep(500);
            if (i == 4) {
                System.out.println("礼让" + t2.getName());
                Thread.yield();
            }
        }
    }
    public static void main1(String[] args) throws InterruptedException {
        T2 t2 = new T2();
        t2.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("main执行第" + (i + 1) + "次");
            Thread.sleep(500);
            if (i == 4) {
                System.out.println("让" + t2.getName() + "插队");
                t2.join();
            }
        }
    }
}
class T2 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(getName() + "执行第" + (i + 1) + "次");
            try {
                sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}